#include "figurecontainer.h"
#include "ellipse.h"
#include "rectangle.h"
#include "triangle.h"

#include <fstream>
#include <stdexcept>

FigureContainer::FigureContainer()
{

}

Figure *FigureContainer::operator[](int i) {
    return container[i];
}

FigureType FigureContainer::type(int i)
{
    return container[i]->type();
}

int FigureContainer::addFigure(FigureType type, std::vector<double> &data)
{
    Figure* fig;
    switch (type) {
        case ell:   fig = new Ellipse(data);    break;
        case rect:  fig = new Rectangle(data);  break;
        case tri:   fig = new Triangle(data);   break;
    }
    container.push_back(fig);
    return container.size() - 1;
}

void FigureContainer::changeData(int i, std::vector<double> &data)
{
    container[i]->setData(data);
}

bool FigureContainer::isEmpty()
{
    if (container.empty())
        return 1;
    else
        return 0;
}

size_t FigureContainer::size()
{
    return container.size();
}

void FigureContainer::extension(int i, double coef)
{
    container[i]->extension(coef);
}

void FigureContainer::extensionAll(double coef)
{
    for (size_t i = 0; i < container.size(); ++i) {
        container[i]->extension(coef);
    }
}

void FigureContainer::shift(int i, double coef1, double coef2)
{
    container[i]->shift(coef1, coef2);
}

void FigureContainer::shiftAll(double coef1, double coef2)
{
    for (size_t i = 0; i < container.size(); ++i) {
        container[i]->shift(coef1, coef2);
    }
}

void FigureContainer::turn(int i, double angle)
{
    container[i]->turn(angle);
}

void FigureContainer::turnAll(double angle)
{
    for (size_t i = 0; i < container.size(); ++i) {
        container[i]->turn(angle);
    }
}

void FigureContainer::remove(int i)
{
    delete container[i];
    container.erase(container.begin() + i);
}

void FigureContainer::removeAll()
{
    for (size_t i = 0; i < container.size(); ++i) {
        delete container[i];
    }
    container.clear();
}

void FigureContainer::inputFile(std::string name)
{
    std::ifstream file;
    file.open(name.c_str());
    if (!file.is_open())
        throw std::logic_error("Input file was not found!");

    int type = 0;
    double buf = 0;
    std::vector<double> data;

    while (!file.eof()) {
        file >> type;
        size_t size = 0;
        switch(type) {
            case 0:
            case 1:
                size = 5;
                break;
            case 2:
                size = 7;
                break;
        }
        for (size_t i = 0; i < size; ++i) {
            file >> buf;
            data.push_back(buf);
        }

        addFigure((FigureType)type, data);
        data.clear();
    }
    file.close();
}

void FigureContainer::outputFile(std::string name)
{
    std::ofstream file;
    file.open(name.c_str(), std::ios::out);
    if (!file.is_open())
        throw std::logic_error("Output file was not found!");

    std::vector<double> tmp;

    for (size_t i = 0; i < container.size(); ++i) {
        file << container[i]->type() << " ";
        tmp = container[i]->getData();
        for (size_t j = 0; j < tmp.size(); ++j) {
            file << tmp[j];
            if (j != tmp.size() - 1)
                file << " ";
        }
        if (i != container.size() - 1)
            file << '\n';
    }

    file.close();
}

FigureContainer::~FigureContainer()
{
    for (size_t i = 0; i < container.size(); ++i) {
        delete container[i];
    }
}
